<extend  name="Public:base" />
<block name="content_header">
    <div class="content-header">
        <h2 class="content-title">公告列表</h2>
        <span class="layui-breadcrumb">
            <a href="{:U('Index/index')}">首页</a>
            <a>文章管理</a>
            <a><cite>公告列表</cite></a>
        </span>
    </div>
</block>
<block name="toolbar">
    <div class="layui-inline">
        标题：<input class="layui-input search-input" type="text" name="title" id="title" placeholder="请输入关键字" />
    </div>
    <div class="layui-inline">
        日期：<input id="searchDate" class="layui-input search-input date-icon" type="text" name="date" placeholder="请选择日期范围" style="width: 175px;" />
    </div>
    <div class="layui-btn-group toolbar">
        <button class="layui-btn" data-type="search">搜索</button>
    </div>
    <div class="layui-btn-group toolbar">
        <button class="layui-btn" data-type="add">新增</button>
    </div>
    <div class="layui-btn-group toolbar">
        <button class="layui-btn" data-type="enable">启用</button>
        <button class="layui-btn layui-btn-warm" data-type="disable">禁用</button>
        <button class="layui-btn layui-btn-danger" data-type="del">删除</button>
    </div>
</block>
<block name="content_body">
    <table class="layui-table" lay-data="{height: 'full-240',cellMinWidth: 80, url:'{:U('Notice/index')}', method:'post', page:{layout: [ 'prev', 'page', 'next', 'skip', 'count']}, id:'dataTable'}" lay-filter="table">
        <thead>
        <tr>
            <th lay-data="{type:'checkbox', fixed: 'left'}" lay-filter='test'></th>
            <th lay-data="{field:'id', width:60, sort: true}">ID</th>
            <th lay-data="{field:'title',width:250}">公告标题</th>
            <th lay-data="{field:'content'}">内容</th>
            <th lay-data="{field:'add_time', width:160,sort:true}">发布时间</th>
            <th lay-data="{width:100, align:'center', templet: '#changeStatusBtn', unresize: true}">状态</th>
            <th lay-data="{fixed: 'right',width:120, align:'center', toolbar: '#handle'}">操作</th>
        </tr>
        </thead>
    </table>
    <script type="text/html" id="changeStatusBtn">
        <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="正常|禁用" lay-filter="changeStatus" {{ d.status == 1 ? 'checked' : '' }}>
    </script>
    <script type="text/html" id="handle">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
</block>
<block name="foot_script">
    <script>
        //JavaScript代码区域
        layui.use(['layer','laydate','table','form'], function(){
            var $ = layui.$, layer = layui.layer,laydate = layui.laydate,table = layui.table,form = layui.form;
            //日期范围
            laydate.render({
                elem: '#searchDate'
                ,range: true
                ,theme: '#393D49'
            });
            // toolbar启用/禁用/删除
            $('.toolbar .layui-btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
            var active = {
                search: function(){
                    var title = $('#title');
                    var date = $('#searchDate');
                    //执行重载
                    table.reload('dataTable', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                            title: title.val(),
                            date: date.val()
                        }
                    });
                }
                ,enable: function(){
                    //启用
                    var url = '{:U("Notice/changeStatus")}';
                    var checkStatus = table.checkStatus('dataTable')
                        ,data = checkStatus.data;
                    if (data.length <= 0){
                        layer.msg("未选择");
                        return;
                    }
                    // 组装ID
                    var ids = [];
                    for (var i in data){
                        ids.push(data[i].id);
                    }
                    $.post(url,{ids:ids,method:'enable'},function (res) {
                        if (res.code === 1){
                            layer.msg(res.msg);
                            table.reload('dataTable');
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                }
                ,disable: function(){
                    //禁用
                    var url = '{:U("Notice/changeStatus")}';
                    var checkStatus = table.checkStatus('dataTable')
                        ,data = checkStatus.data;
                    if (data.length <= 0){
                        layer.msg("未选择");
                        return;
                    }
                    // 组装ID
                    var ids = [];
                    for (var i in data){
                        ids.push(data[i].id);
                    }
                    $.post(url,{ids:ids,method:'disable'},function (res) {
                        if (res.code === 1){
                            layer.msg(res.msg);
                            table.reload('dataTable');
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                }
                ,del: function(){
                    //删除
                    var url = '{:U("Notice/changeStatus")}';
                    var checkStatus = table.checkStatus('dataTable')
                        ,data = checkStatus.data;
                    if (data.length <= 0){
                        layer.msg("未选择");
                        return;
                    }
                    layer.confirm('确定删除吗?', {icon: 3, title:'提示'}, function(index){
                        // 组装ID
                        var ids = [];
                        for (var i in data){
                            ids.push(data[i].id);
                        }
                        $.post(url,{ids:ids,method:'del'},function (res) {
                            if (res.code === 1){
                                layer.msg(res.msg);
                                layer.close(index);
                                table.reload('dataTable');
                            } else {
                                layer.msg(res.msg);
                            }
                        });
                    });
                }
                ,add: function () {
                    //新增
                    openIframe('新增公告','{:U("Notice/add")}',['900px','600px']);
                }
            };

            table.on('row(table)', function(obj){
                console.log(obj.data)
            });
            //监听操作
            form.on('switch(changeStatus)', function(obj){
                var ids = [this.value];
                var url = '{:U("Notice/changeStatus")}';
                if (obj.elem.checked){
                    // 启用
                    $.post(url,{ids:ids,method:'enable'},function (res) {
                        if (res.code === 1){
                            layer.msg(res.msg);
                        } else {
                            layer.msg(res.msg);
                            table.reload('dataTable');
                        }
                    });
                } else {
                    // 禁用
                    $.post(url,{ids:ids,method:'disable'},function (res) {
                        if (res.code === 1){
                            layer.msg(res.msg);
                        } else {
                            layer.msg(res.msg);
                            table.reload('dataTable');
                        }
                    });
                }
            });

            //监听工具条
            table.on('tool(table)', function(obj){
                var data = obj.data;
                if(obj.event === 'edit'){
                    var url = '{:U("Notice/edit")}';
                    if (url.indexOf('?') > 0){
                        url = url + '&id=' + data.id;
                    } else {
                        url = url + '?id=' + data.id;
                    }
                    var inde = openIframe('编辑公告',url,['900px','600px']);

                } else if(obj.event === 'del'){
                    var url = '{:U("Notice/changeStatus")}';
                    layer.confirm('真的删除吗？', function(index){
                        // 组装ID
                        var ids = [data.id];
                        $.post(url,{ids:ids,method:'del'},function (res) {
                            if (res.code === 1){
                                layer.msg(res.msg);
                                layer.close(index);
                                table.reload('dataTable');
                            } else {
                                layer.msg(res.msg);
                            }
                        });
                    });
                }
            });
            var check = table.checkStatus('table');
            console.log(check.data.length);
        });


    </script>
</block>